// pages/OrderDetails/OrderDetails.js
const db = wx.cloud.database()
const cont = db.collection('Order')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    Orders:[],
    show:false,
    radioChange:''
  },
  fh:function(){
    wx.navigateBack({
      delta: 1
    })
  },
  lt:function(){
    console.log("dsfdfdfsd")
    wx.navigateTo({
      url: "/pages/ChatRoom/ChatRoom?id=" + this.data.Orders[0].GoodsId + "",
    })
  },
  radioChange:function(){
    this.setData({
      radioChange:'已选'
    })
  },
  ckwl:function(e){
    var OrderId=this.data.Orders[0].id
    var CourierNumber=this.data.Orders[0].CourierNumber
    wx.navigateTo({
      url: "/pages/express/express?OrderId=" + OrderId +"&CourierNumber="+CourierNumber,
    })
  },
  ConfirmReceipt:function(e){
    var that=this
    wx.showModal({
      title: '确认收货',
      content: '是否确认付款',
      success: function (res) {
        if (res.confirm) {
          db.collection('Order').doc(e.currentTarget.dataset.id).update({
            data: {
              // 表示指示数据库将字段自增 10
              state: '待评价',
              ReceivingTime:that.getNowDatTime()
            },
            success: function(res) {
              console.log(res.data)
            }
          })
        } else {
          console.log(res.confirm)
        }
      }
    })
  },
  //获取当前时间
  getNowDatTime:function(){
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
      month = "0" + month;
    }
    if (strDate >= 0 && strDate<=9) {
      strDate = "0" + strDate;
    }
    var time = date.getFullYear() + seperator1 + month + seperator1 + strDate + " " + date.getHours() + seperator2 + date.getMinutes() + seperator2 + date.getSeconds();
    console.log(time);
    return time;
  },
  Evaluation:function(e){
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: "/pages/Evaluate/Evaluate?id=" + e.currentTarget.dataset.id +"",
    })
  },
  ViewEvaluation:function(e){
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: "/pages/ViewEvaluation/ViewEvaluation?OrderId=" + e.currentTarget.dataset.id +"",
    })
  },
  fk:function(e){
    console.log(e.currentTarget.dataset.id)
    var that=this
    wx.showModal({
      title: '付款',
      content: '是否进行支付',
      success: function (res) {
        if (res.confirm) {
          db.collection('Order').doc(e.currentTarget.dataset.id).update({
            data: {
              // 表示指示数据库将字段自增 10
              state: '待发货',
              PaymentTime:that.getNowDatTime()
            },
            success: function(res) {
              console.log(res.data)
            }
          })
        } else {
          console.log(res.confirm)
        }
      }
    })
  },
  qx:function(){
    if(this.data.radioChange=='已选'){
      this.show()
      db.collection('Order').doc(this.data.DelectId).remove({
        success: function(res) {
          console.log(res.data)
        }
      })
    }else{
      wx.showToast({
        title: "请选择取消订单的原因",
        icon: "none"
      })
    }
  },
  show:function(){
    this.setData({
      show:!this.data.show
    })
  },
  show2:function(e){
    console.log(e.currentTarget.dataset.id)
    this.setData({
      show:!this.data.show,
      DelectId:e.currentTarget.dataset.id
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  getOrder:function(OrderId){
    var that=this
    cont.doc(OrderId).get()
      .then(res => {
        // res.data 包含该记录的数据
        console.log(res.data)
        var id = "Orders[0].id"
          var Consignee = "Orders[0].Consignee"
          var DeliveryTime = "Orders[0].DeliveryTime"
          var GoodsId = "Orders[0].GoodsId"
          var ReceivingTime = "Orders[0].ReceivingTime"
          var CreationTime = "Orders[0].CreationTime"
          var PaymentTime = "Orders[0].PaymentTime"
          var Remarks = "Orders[0].Remarks"
          var SellOpenid = "Orders[0].SellOpenid"
          var Telephone = "Orders[0].Telephone"
          var openid = "Orders[0].openid"
          var address = "Orders[0].address"
          var color = "Orders[0].color"
          var number = "Orders[0].number"
          var rmb = "Orders[0].rmb"
          var rmb3 = "Orders[0].rmb3"
          var rmb2 = "Orders[0].rmb2"
          var size = "Orders[0].size"
          var state = "Orders[0].state"
          var GoodsTitle = "Orders[0].GoodsTitle"
          var SellNickName = "Orders[0].SellNickName"
          var SellAvatarUrl = "Orders[0].SellAvatarUrl"
          var GoodsImg = "Orders[0].GoodsImg"
          var express = "Orders[0].express"
          var CourierNumber="Orders[0].CourierNumber"
          
          that.setData({
            [id] :res.data._id,
            [Consignee]:res.data.Consignee,
            [DeliveryTime]:res.data.DeliveryTime,
            [GoodsId]:res.data.GoodsId,
            [ReceivingTime]:res.data.ReceivingTime,
            [CreationTime]:res.data.CreationTime,
            [PaymentTime]:res.data.PaymentTime,
            [Remarks]:res.data.Remarks,
            [SellOpenid]:res.data.SellOpenid,
            [Telephone]:res.data.Telephone,
            [openid]:res.data._openid,
            [address]:res.data.address,
            [color]:res.data.color,
            [number]:res.data.number,
            [rmb]:(res.data.rmb).toFixed(2),
            [rmb3]:(res.data.rmb*res.data.number).toFixed(2),
            [rmb2]:(res.data.rmb*res.data.number+res.data.express).toFixed(2),
            [size]:res.data.size,
            [state]:res.data.state,
            [GoodsTitle]:res.data.GoodsTitle,
            [SellNickName]:res.data.SellNickName,
            [SellAvatarUrl]:res.data.SellAvatarUrl,
            [GoodsImg]:res.data.GoodsImg,
            [express]:res.data.express,
            [CourierNumber]:res.data.CourierNumber,
          })
        
      })
      .catch(err => {
        console.error(err)
    })
  },
  onLoad: function (options) {
    console.log(options.id)
    this.getOrder(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})